package cn.wuyi.common.util;


import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.text.SimpleDateFormat;

public class JacksonMapper {
	
	/**
	 * 每个线程一个共享的ObjectMapper
	 */
	private static ThreadLocal<ObjectMapper> threadLocal = new ThreadLocal<ObjectMapper>();
	
	/**
	 * 默认空的构造方法
	 */
	private JacksonMapper() {
		
	}

	/**
	 * @方法功能说明：创建每个线程共享的ObjectMapper
	 * @作者        : 
	 * @param include
	 * @return
	 */
	public static ObjectMapper getInstance(Include include) {
		if(threadLocal.get() == null){
			ObjectMapper newObjectMapper = new ObjectMapper();
			if (include != null) {
				// 设置输出时包含属性的风格
				newObjectMapper.setSerializationInclusion(include);
			}
			// 设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
			newObjectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
			// 设置默认DateFormat yyyy-MM-dd HH:mm:ss
			newObjectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
			threadLocal.set(newObjectMapper);
		}
		return threadLocal.get();
	}
}
